package com.supor.aiot.md_msg.ui;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.android.baseconfig.base.RxBus3;
import com.android.baseconfig.common.utils.Util;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.arouter.PathUrl;
import com.supor.aiot.common.base.BaseMVVMEnhanceActivity;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.common.helper.GlobalDataCache;
import com.supor.aiot.md_msg.R;
import com.supor.aiot.md_msg.adapter.CommunityMsgListAdapter;
import com.supor.aiot.md_msg.databinding.ActivityLikeMsgBinding;
import com.supor.aiot.md_msg.entity.AddrJsonBean;
import com.supor.aiot.md_msg.entity.MsgCenterListBean;
import com.supor.aiot.md_msg.entity.MsgCenterListResultBean;
import com.supor.aiot.md_msg.model.MsgCenterModel;

public class LikeMsgActivity extends BaseMVVMEnhanceActivity<MsgCenterModel, ActivityLikeMsgBinding> implements OnItemClickListener, OnItemChildClickListener {
    private int currentListPage;
    private CommunityMsgListAdapter msgListAdapter;

    @Override
    protected void initView() {
        super.initView();
        toolBar();
    }

    @Override
    protected void initData() {
        super.initData();
        msgListAdapter = new CommunityMsgListAdapter();
        bindingView.rvContent.setLayoutManager(new LinearLayoutManager(this));
        bindingView.rvContent.setAdapter(msgListAdapter);
        msgListAdapter.setOnItemClickListener(this);
        msgListAdapter.setOnItemChildClickListener(this);
//        msgListAdapter.setEmptyView(R.layout.layout_msg_skele);

        bindingView.smartRefreshLayout.setEnableRefresh(false);
        bindingView.smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.getLikeMsgList(GlobalDataCache.userInfo.getBossUserId(),
                        ++currentListPage,
                        bindDestoryAutoDispose()
                );
            }
        });
        viewModel.getLikeMsgListResult().observe(this, new Observer<Result<MsgCenterListResultBean>>() {
            @Override
            public void onChanged(Result<MsgCenterListResultBean> msgCenterListResultBeanResult) {
                closeLoading();
                if (null != msgCenterListResultBeanResult.getData()) {
                    msgListAdapter.addData(msgCenterListResultBeanResult.getData().getRows());
                }
                bindingView.smartRefreshLayout.finishLoadMore();
                if (msgCenterListResultBeanResult.getData().getPage() >= msgCenterListResultBeanResult.getData().getAllpages()) {
                    bindingView.smartRefreshLayout.setEnableLoadMore(false);
                }
                if (currentListPage == 1 && (null == msgCenterListResultBeanResult.getData() || msgCenterListResultBeanResult.getData().getTotal() == 0)) {
                    msgListAdapter.setEmptyView(R.layout.layout_msg_empty);
                }
            }
        });
        showLoading();
        viewModel.getLikeMsgList(GlobalDataCache.userInfo.getBossUserId(),
                ++currentListPage,
                bindDestoryAutoDispose()
        );
    }

    private void toolBar() {
        bindingView.classifyToolbar.setTitle("收到的赞");
        bindingView.classifyToolbar.setTitleTextSize(Util.sp2px(this, 17));
        bindingView.classifyToolbar.setTitleColor(0xFF333333);
        bindingView.classifyToolbar.setIvLeftOnClickListener(view -> finish());
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (msgListAdapter.getData().size() > position) {
            RxBus3.getInstance().post(Constants.Tag.MSG_COMM_READ, Constants.Value.SUCCESS);
            viewModel.readMsg(String.valueOf(msgListAdapter.getData().get(position).getAutoId()), bindDestoryAutoDispose());
            View viewUnread = msgListAdapter.getViewByPosition(position, R.id.view_unread);
            if (null != viewUnread) {
                viewUnread.setVisibility(View.GONE);
            }
//            if (null != msgListAdapter.getData().get(position).getAddrJson()) {
////                6. /pages/communityHome/talkDetail？e='此动态id'&f='此动态发布者usersId' &id='此评论id'
//                AddrJsonBean addrJsonBean = msgListAdapter.getData().get(position).getAddrJson();
//                String url = "http://140.207.48.210:30083/supor-h5/#/pages/communityHome/talkDetail?e="
//                        + addrJsonBean.getToId()
//                        + "&f=" + addrJsonBean.getBossUserId()
//                        + "&id=" + addrJsonBean.getFromId();
//                ARouter.getInstance().build(PathUrl.PATH_WEB_VIEW)
//                        .withString("URL_PATH", url)
//                        .withTransition(com.android.baseconfig.R.anim.activity_open_enter,
//                                com.android.baseconfig.R.anim.activity_open_exit)
//                        .navigation(LikeMsgActivity.this);
//            }
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        if (msgListAdapter.getData().size() > position) {
            if (R.id.ll_content == view.getId()) {
                RxBus3.getInstance().post(Constants.Tag.MSG_COMM_READ, Constants.Value.SUCCESS);
                viewModel.readMsg(String.valueOf(msgListAdapter.getData().get(position).getAutoId()), bindDestoryAutoDispose());
                View viewUnread = msgListAdapter.getViewByPosition(position, R.id.view_unread);
                if (null != viewUnread) {
                    viewUnread.setVisibility(View.GONE);
                }
            } else if (R.id.ll_right_del == view.getId()) {
                viewModel.delMsg(String.valueOf(msgListAdapter.getData().get(position).getAutoId()),
                        bindDestoryAutoDispose());
                msgListAdapter.removeAt(position);
            }
//            if (null != msgListAdapter.getData().get(position).getAddrJson()) {
////                6. /pages/communityHome/talkDetail？e='此动态id'&f='此动态发布者usersId' &id='此评论id'
//                AddrJsonBean addrJsonBean = msgListAdapter.getData().get(position).getAddrJson();
//                String url = "http://140.207.48.210:30083/supor-h5/#/pages/communityHome/talkDetail?e="
//                        + addrJsonBean.getToId()
//                        + "&f=" + addrJsonBean.getBossUserId()
//                        + "&id=" + addrJsonBean.getFromId();
//                ARouter.getInstance().build(PathUrl.PATH_WEB_VIEW)
//                        .withString("URL_PATH", url)
//                        .withTransition(com.android.baseconfig.R.anim.activity_open_enter,
//                                com.android.baseconfig.R.anim.activity_open_exit)
//                        .navigation(LikeMsgActivity.this);
//            }
        }
    }
}